package com.shujia

import scala.io.Source

object Code36Join {
  def main(args: Array[String]): Unit = {
    val stuInfoList: List[(String, String, Int, String, String)] = Source
      .fromFile("scala_code/data/students_join.txt")
      .getLines()
      // 将读取到的每一行数据转换成List中的元素
      .toList
      .map(
        (line: String) => {
          val colum: Array[String] = line.split(",")
          // (id,name,age,gender,clazz)
          //  Tuple相比较Array 其元素的数据类型可以多样
          (colum(0), colum(1), colum(2).toInt, colum(3), colum(4))
        }
      )

    val scoreList: List[(String, String, String)] = Source
      .fromFile("scala_code/data/score_join.txt")
      .getLines()
      // 将读取到的每一行数据转换成List中的元素
      .toList
      .map(
        line => {
          val column: Array[String] = line.split(",")
          (column(0), column(1), column(2))
        }
      )


    stuInfoList
      .flatMap(
        info => {
          val joinRes: List[(String, String, String)] = scoreList.filter(
            score => {
              // 关联条件
              score._1 == info._1
            }
          )
          joinRes.map(
            oneScore => (info._1, info._2, info._3, info._4, info._5, oneScore._2, oneScore._3)
          )
        }
      )
      .foreach(println)
  }
}
